package someTestExcemple.ioStream;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

public class PropertiesTest {
    public static void main(String[] args) throws IOException {
        test1();
    }
    //用Properties类来读取properties文件(属性文件)
    public static void test1() throws IOException {
        Properties properties = new Properties();
        //加载文件中的键值对数据到properties对象中
        properties.load(new FileReader("src/main/java/someTestExcemple/ioStream/users.properties"));
        System.out.println(properties);
        properties.forEach((k, v) -> System.out.println(k + "=" + v));
        //获取所以键
        Set<String> keys = properties.stringPropertyNames();
        System.out.println("---------------------------------------");
        for (String key : keys) {
            System.out.println(key + "=" + properties.getProperty(key));
        }
        System.out.println("1---------------------------------------");
        if(properties.containsKey("admin")) {
            properties.setProperty("admin", "2324234523");
            properties.store(new FileWriter("src/main/java/someTestExcemple/ioStream/users.properties"), "change");
        }

    }

}
